package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.LpcProduct;
import com.gift.entity.UserMeans;
import com.gift.mapper.GiProductMapper;
import com.gift.mapper.LpcProductMapper;
import com.gift.mapper.LpcTypeMapper;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/lpc-product")
public class LpcProductController extends JSONAuthentication {
    @Autowired
    LpcProductMapper lpcProductMapper;
    @Autowired
    LpcTypeMapper lpcTypeMapper;
    @Autowired
    GiProductMapper giProductMapper;
    @ApiOperation(value = "设置礼品册价格对应可兑换的商品")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody LpcProduct lpcProduct) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("price",lpcProduct.getPrice());
        wrapper.eq("pr_id",lpcProduct.getPrId());
        LpcProduct lpcProduct1 = lpcProductMapper.selectOne(wrapper);
        if(lpcProduct1!=null){
            Result error = Result.error().message("请勿重复添加");
            this.WriteJSON(request, response, error);
        }else{
            if(lpcProductMapper.insert(lpcProduct)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除礼品册兑换对应商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = lpcProductMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据礼品册价格查询可兑换的商品")
    @PostMapping("/findByLpcPrice")
    public void findByLpcPrice(HttpServletRequest request, HttpServletResponse response, @RequestParam("price") int price,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("price", price);
        Page<LpcProduct> page=new Page<>(current,size);
        Page<LpcProduct> pages=lpcProductMapper.selectPage(page,wrapper);
        List<LpcProduct> list=pages.getRecords();
        int total=lpcProductMapper.selectCount(wrapper);
        for (LpcProduct a:list){
            a.setGiProduct(giProductMapper.selectById(a.getPrId()));
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("price",a.getPrice());
            a.setLpcType(lpcTypeMapper.selectOne(wrapper1));

        }
        if (list.size()>0) {
            Result ok = Result.ok().data("lpc", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部礼品册可兑换的商品")
    @PostMapping("/findByLpcAll")
    public void findByLpcAll(HttpServletRequest request, HttpServletResponse response,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<LpcProduct> page=new Page<>(current,size);
        Page<LpcProduct> pages=lpcProductMapper.selectPage(page,null);
        List<LpcProduct> list=pages.getRecords();
        int total=lpcProductMapper.selectCount(null);
        for (LpcProduct a:list){
            a.setGiProduct(giProductMapper.selectById(a.getPrId()));
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("price",a.getPrice());
            a.setLpcType(lpcTypeMapper.selectOne(wrapper1));

        }
        if (list.size()>0) {
            Result ok = Result.ok().data("lpc", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}



